package com.baidu.infinity.ui.fragment.home.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.baidu.infinity.R
import com.baidu.infinity.ui.fragment.home.adapter.ColorRecyclerAdapter
import com.baidu.infinity.ui.fragment.home.shape.LayerManager
import com.baidu.infinity.ui.util.getSomeColor

class HSVColorPickerDialog(
    context: Context,
    attrs: AttributeSet?
): FrameLayout(context,attrs) {
    private lateinit var colorPickerView: ColorPickerView
    private lateinit var sturationBarView: SeekBar
    private lateinit var tightnessView: SeekBar
    private lateinit var tvSturationView: TextView
    private lateinit var tvTightnessView: TextView
    private lateinit var recyclerView: RecyclerView
    private var listener: (color:Int)->Unit = {}
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        colorPickerView = findViewById<ColorPickerView>(R.id.colorPickerView)
        sturationBarView = findViewById<SeekBar>(R.id.sturationBar)
        tightnessView = findViewById<SeekBar>(R.id.tightnessBar)
        tvSturationView = findViewById<TextView>(R.id.tvSturation)
        tvTightnessView = findViewById<TextView>(R.id.tvTightness)
        recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        colorPickerView.addColorChangeListener(this.listener)
        sturationBarView.setOnSeekBarChangeListener(object : OnSeekBarChangeListener{
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                tvSturationView.setText(progress.toString())
                colorPickerView.changeSturation(progress)
            }
            override fun onStartTrackingTouch(seekBar: SeekBar?) {}
            override fun onStopTrackingTouch(seekBar: SeekBar?) {}
        })
        tightnessView.setOnSeekBarChangeListener(object : OnSeekBarChangeListener{
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                tvTightnessView.setText(progress.toString())
                colorPickerView.changeTightness(progress)

            }
            override fun onStartTrackingTouch(seekBar: SeekBar?) {}
            override fun onStopTrackingTouch(seekBar: SeekBar?) {}
        })
        val adapter = ColorRecyclerAdapter()
        recyclerView.adapter = adapter
        adapter.currentColorListener = {color ->
            listener(color)
        }
        recyclerView.layoutManager = GridLayoutManager(context,5,RecyclerView.VERTICAL,false)
        adapter.setNewData(getSomeColor())
    }
    fun addColorChangeListener(listener: (color:Int)->Unit){
        this.listener = listener
    }
}